#include <iostream>
#include <thread>
#include <future>

int add(int a,int b)
{
    std::cout << "加法" << "," << std::this_thread::get_id() << '\n';
    return a + b;
}

int main()
{
    //std::launch::deferred 会在调用get时,执行异步任务
    // std::future<int> fu = std::async(std::launch::deferred,add,10,20);

    //std::launch::async 会立即创建线程,执行异步任务
    std::future<int> fu = std::async(std::launch::async,add,10,20);

    std::cout << "主线程: " << std::this_thread::get_id() << "\n";

    //主线程休眠两秒
    std::this_thread::sleep_for(std::chrono::seconds(2));

    //获取异步任务结果
    int res = fu.get();
    std::cout << res << std::endl;

    return 0;
}